package com.szholly.plug.safe.login;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.utils.common.log.LogFactory;
import com.szholly.utils.session.SessionFactory;
import com.szholly.utils.session.provider.ISessionProvider;
import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.utils.util.CookieUtils;
import com.szholly.utils.util.DateUtils;
import com.szholly.plug.safe.entity.LogEntity;
import com.szholly.plug.safe.entity.LogService;
import com.szholly.plug.safe.entity.LogTypeDic;
import com.szholly.plug.safe.entity.user.UserEntity;

@Controller
public class LoginController {

	/**
	 * 退出登录
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/login/loginOut.do")
	public String loginOut(HttpServletRequest request,
			HttpServletResponse response) {
		ISessionProvider session = SessionFactory.getSession();
		logLogout(session);
		if (session != null) {
			session.logout(request, response);
			session.clear(request.getSession());
		}
		request.getSession().invalidate();
		CookieUtils.cancleCookie(request, response, CookieUtils.Security_Tag, null);
		CookieUtils.cancleCookie(request, response, CookieUtils.Security_Url, null);
		
		LoginParameter loginParameter = (LoginParameter) SpringBeanFactory
				.getBean("loginParameter");

		return loginParameter.getLoginOutUrl();
	}

	private void logLogout(ISessionProvider session) {
		if(!LogFactory.getlogPara().isLogOut()){
			return;
		}
		
		UserEntity userEntity = null;
		LogEntity log = new LogEntity();
		if (session != null) {
			log.setIP(session.getIp());
			userEntity = (UserEntity) session.getObject(ISessionProvider.UserEntity);
		}
		log.setLogDate(DateUtils.convertDateTimeToString(null));
		log.setLogDesc("退出系统");
		log.setLogType(LogTypeDic.LogOut);
		if (userEntity != null) {
			log.setUserName(userEntity.getUserRealName());
		}
		LogService.getSingleRef().SaveEntityInThread(log);
	}

	/**
	 * 登录
	 * @throws IOException 
	 */
	@RequestMapping(value = "/gs/login.do")
	public String login(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		Object obj = request.getSession().getAttribute(
				"SPRING_SECURITY_LAST_EXCEPTION");
		if (obj instanceof Exception) {
			model.addAttribute("error","用户名或密码错误！");
		}
		return "view/gs/login";
	}
	@RequestMapping(value = "/ws/login.do")
	public String wslogin(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
			Object obj = request.getSession().getAttribute(
					"SPRING_SECURITY_LAST_EXCEPTION");
			if (obj instanceof Exception) {
				model.addAttribute("error","用户名或密码错误！");
			}
		return "view/ws/login";
	}
	
	@RequestMapping(value = "/sw/login.do")
	public String swlogin(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		Object obj = request.getSession().getAttribute(
				"SPRING_SECURITY_LAST_EXCEPTION");
		if (obj instanceof Exception) {
			model.addAttribute("error","用户名或密码错误！");
		}
		return "view/sw/login";
	}
	
	@RequestMapping(value = "/wdp/login.do")
	public String wdplogin(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		Object obj = request.getSession().getAttribute(
				"SPRING_SECURITY_LAST_EXCEPTION");
		if (obj instanceof Exception) {
			model.addAttribute("error","用户名或密码错误！");
		}
		return "view/wdp/login";
	}
}